
// demo1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "demo1.h"
#include "demo1Dlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About


class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

	// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

	// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
	EnableActiveAccessibility();
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)

END_MESSAGE_MAP()



// Cdemo1Dlg dialog




Cdemo1Dlg::Cdemo1Dlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(Cdemo1Dlg::IDD, pParent)
{
	EnableActiveAccessibility();
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pButtonControls =  NULL;
	

}

void Cdemo1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);


	DDX_Control(pDX, btn_1, m_btn1);
}

BEGIN_MESSAGE_MAP(Cdemo1Dlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, &Cdemo1Dlg::OnBnClickedOk)
	ON_BN_CLICKED(btn_1, &Cdemo1Dlg::OnBnClicked1)
	ON_CONTROL_RANGE(BN_CLICKED,2000,2100,OnAnyButton)
	ON_CONTROL_RANGE(BN_CLICKED,100,110,OnAnyButton)

END_MESSAGE_MAP()


// Cdemo1Dlg message handlers

BOOL Cdemo1Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);



	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	//m_btn1.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP1)));

	//m_colorBtn.Attach(btn_1, this); // CCT

	/*CString str;
	int _width = 132;
	int _height = 32;
	int _x = 0;
	int _y = 0;
	for(int i = 0; i < 10; i++)
	{
		m_ButtonArray[i] = new CButton;
		str.Format(L"Button%d",i+1);

		DWORD dwStyle = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON;
		m_ButtonArray[i]->Create(L" ", dwStyle, CRect(_x,_y,_x+_width,_y+_height), this,100+i);
		m_ButtonArray[i]->SetBitmap(LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP1)));
		m_ButtonArray[i]->ShowWindow(TRUE);

		_y += _height;
		if((i+1) % 10 == 0){
			_x += _width;
			_y = 0;
		}
	}*/


	return TRUE;  // return TRUE  unless you set the focus to a control
}

void Cdemo1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void Cdemo1Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR Cdemo1Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void Cdemo1Dlg::OnBnClickedOk()
{
	// base ID for the new button controls, make sure the number you choose
	// does not conflict with other control ID's already in the dialog
	const int IDC_BTN_CREATED1 = 2000;

	// say, IDC_EDIT1 is the edit box where they enter the number of 
	// controls to create
	//int nCount = GetDlgItemInt(IDC_EDIT1, NULL, FALSE);
	int nCount = 100;
	if(nCount > 0)
	{
		//CWnd* pEdit1 = GetDlgItem(IDC_EDIT1);


		// you probably want to use the same font in those new dialogs as the one already
		// in IDC_EDIT1, so get it
		//CFont* pFont = pEdit1->GetFont();

		// also, you probably want to use the same width and height as well
		//CRect rc;
		//pEdit1->GetWindowRect(rc);
		// int nEditWidth = rc.Width();
		//int nEditHeight = rc.Height();

		int nEditWidth = 32;
		int nEditHeight = 32;

		// now allocate your array

		m_pButtonControls = new CButton[nCount];
		//m_pButtonControls = new CColorButton[nCount];

		// I'm going to assume the controls ar e created at position 0,0
		// going down, change that to suit your need

		int y = 0;
		int x = 0;
		CString str;
		for(int i = 0; i < nCount ; i++)
		{
			// position and size (notice, CWnd::CreateEx wants to rect as left, top, right, bottom
			// unlike CreateWindow or CreateWindowEx where it should be left, top, width, height
			CRect rect(x, y, x + nEditWidth, y + nEditHeight);

			//str.Format(L"Button%d",i+1);

			

			m_pButtonControls[i].Create(         
				L"\n",         
				WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_EX_TRANSPARENT,
				rect,
				this,
				IDC_BTN_CREATED1 + i);

			//y += nEditHeight * 2;
			y += nEditHeight;

			if((i+1) % 10 == 0){
				x += nEditWidth;
				y = 0;
			}

			m_pButtonControls[i].SetBitmap(LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_normalCell)));
			
			//m_pButtonControls[i].Attach(IDC_BTN_CREATED1 + i, this); // CCT
			
			//m_pButtonControls[i].SetColor(BLUE,BLUE);

			

			
			// set the font
			//m_pButtonControls[i].SetFont(GetDlgItem(IDC_EDIT1)->GetFont());
		}

		// m_btnDynamic.Create(L"Dynamic", WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, CRect(100,150,200,200),this, DYNAMIC_BUTTON_ID);
	}


	//CDialogEx::OnOK();
}



void Cdemo1Dlg::OnBnClicked1()
{
	//AfxMessageBox(L"abc");
	//m_pButtonControls[0].SetBitmap(LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP2)));
	//m_ButtonArray[0]->SetBitmap(LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP2)));

	m_pButtonControls[0].SetIcon(LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME)));
}



// event handle for the buttons
void Cdemo1Dlg::OnAnyButton( UINT nID ) 
{
	CString s;
	s.Format(L"button %d was pressed", nID);
	AfxMessageBox(s);	
	m_pButtonControls[nID - 2000].SetBitmap(LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_emptyCell)));
	
}


//HBRUSH CMyButton::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
//{
//	HBRUSH hbr = CButton::OnCtlColor(pDC, pWnd, nCtlColor);
//	
//	// TODO: Change any attributes of the DC here
//	
//	// TODO: Return a different brush if the default is not desired
//	return hbr;
//}
